﻿/*************************************************************************
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CatchUpTv.Tv;
using System.Net;
using log4net;
using Newtonsoft.Json;
using CatchUpTv.Utilities;

namespace CatchUpTv.Downloader.Australia.AbcIView
{
    /// <summary>
    /// A category in iView.
    /// </summary>
    public class Category
    {
        /// <summary>
        /// The logger for this class.
        /// </summary>
        static ILog log = LogManager.GetLogger(typeof(Category));

        /// <summary>
        /// The iView configuration.
        /// </summary>
        Configuration _configuration;

        public Category(Configuration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// The Id for the category.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// The name for the category.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// A flag indicating if the category is a genre.
        /// </summary>
        public bool Genre { get; set; }

        /// <summary>
        /// Gets the list of shows under this category.
        /// </summary>
        /// <returns>The list of shows.</returns>
        public IEnumerable<Show> GetShows()
        {
            WebClient webClient = new WebClient();
            
            // Get the series list
            var seriesJson = webClient.DownloadString(_configuration.ApiUrl + "keyword=" + Id);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Series JSON: '{0}'", seriesJson);
            }

            List<Series> seriesList = JsonConvert.DeserializeObject<List<Series>>(seriesJson);

            IList<Show> shows = new List<Show>();

            foreach (Series series in seriesList)
            {
                shows.Add(new Show()
                {
                    Id = "ABC_iView_show_" + series.Id,
                    Tag = series,
                    Name = series.Name,
                    Episodes = new Lazy<IEnumerable<Episode>>(series.GetEpisodes)
                });
            }

            return shows;
        }
    }
}
